home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / upnp_coherence / UpnpSource.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.4 KB  |  147 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import rb
  5. import rhythmdb
  6. import gobject
  7. import gtk
  8. from coherence import __version_info__ as coherence_version
  9. from coherence import log
  10. from coherence.upnp.core import DIDLLite
  11.  
  12. class UpnpSource(rb.BrowserSource, log.Loggable):
  13.     logCategory = 'rb_media_store'
  14.     __gproperties__ = {
  15.         'plugin': (rb.Plugin, 'plugin', 'plugin', gobject.PARAM_WRITABLE | gobject.PARAM_CONSTRUCT_ONLY),
  16.         'client': (gobject.TYPE_PYOBJECT, 'client', 'client', gobject.PARAM_WRITABLE | gobject.PARAM_CONSTRUCT_ONLY),
  17.         'udn': (gobject.TYPE_PYOBJECT, 'udn', 'udn', gobject.PARAM_WRITABLE | gobject.PARAM_CONSTRUCT_ONLY) }
  18.     
  19.     def __init__(self):
  20.         rb.BrowserSource.__init__(self)
  21.         self._UpnpSource__db = None
  22.         self._UpnpSource__activated = False
  23.         self.container_watch = []
  24.         if coherence_version < (0, 5, 1):
  25.             self.process_media_server_browse = self.old_process_media_server_browse
  26.         else:
  27.             self.process_media_server_browse = self.new_process_media_server_browse
  28.  
  29.     
  30.     def do_set_property(self, property, value):
  31.         if property.name == 'plugin':
  32.             self._UpnpSource__plugin = value
  33.         elif property.name == 'client':
  34.             self._UpnpSource__client = value
  35.             self.props.name = self._UpnpSource__client.device.get_friendly_name()
  36.         elif property.name == 'udn':
  37.             self._UpnpSource__udn = value
  38.         else:
  39.             raise AttributeError, 'unknown property %s' % property.name
  40.         return property.name == 'plugin'
  41.  
  42.     
  43.     def do_impl_activate(self):
  44.         if not self._UpnpSource__activated:
  45.             print 'activating upnp source'
  46.             self._UpnpSource__activated = True
  47.             shell = self.get_property('shell')
  48.             self._UpnpSource__db = shell.get_property('db')
  49.             self._UpnpSource__entry_type = self.get_property('entry-type')
  50.             self.load_db(0)
  51.             self._UpnpSource__client.content_directory.subscribe_for_variable('ContainerUpdateIDs', self.state_variable_change)
  52.             self._UpnpSource__client.content_directory.subscribe_for_variable('SystemUpdateID', self.state_variable_change)
  53.         
  54.  
  55.     
  56.     def load_db(self, id):
  57.         d = self._UpnpSource__client.content_directory.browse(id, browse_flag = 'BrowseDirectChildren', process_result = False, backward_compatibility = False)
  58.         d.addCallback(self.process_media_server_browse, self._UpnpSource__udn)
  59.  
  60.     
  61.     def state_variable_change(self, variable, udn = None):
  62.         self.info('%s changed from >%s< to >%s<', variable.name, variable.old_value, variable.value)
  63.         if variable.old_value == '':
  64.             return None
  65.         if variable.name == 'SystemUpdateID':
  66.             self.load_db(0)
  67.         elif variable.name == 'ContainerUpdateIDs':
  68.             changes = variable.value.split(',')
  69.             while len(changes) > 1:
  70.                 container = changes.pop(0).strip()
  71.                 update_id = changes.pop(0).strip()
  72.                 if container in self.container_watch:
  73.                     self.info('we have a change in %r, container needs a reload', container)
  74.                     self.load_db(container)
  75.                     continue
  76.         
  77.  
  78.     
  79.     def new_process_media_server_browse(self, results, udn):
  80.         didl = DIDLLite.DIDLElement.fromString(results['Result'])
  81.         for item in didl.getItems():
  82.             self.info('process_media_server_browse %r %r', item.id, item)
  83.             if item.upnp_class.startswith('object.container'):
  84.                 self.load_db(item.id)
  85.             
  86.             if item.upnp_class.startswith('object.item.audioItem'):
  87.                 url = None
  88.                 duration = None
  89.                 size = None
  90.                 bitrate = None
  91.                 for res in item.res:
  92.                     (remote_protocol, remote_network, remote_content_format, remote_flags) = res.protocolInfo.split(':')
  93.                     self.info('%r %r %r %r', remote_protocol, remote_network, remote_content_format, remote_flags)
  94.                     if remote_protocol == 'http-get':
  95.                         url = res.data
  96.                         duration = res.duration
  97.                         size = res.size
  98.                         bitrate = res.bitrate
  99.                         break
  100.                         continue
  101.                 
  102.                 if url is not None:
  103.                     self.info('url %r %r', url, item.title)
  104.                     entry = self._UpnpSource__db.entry_lookup_by_location(url)
  105.                     if entry == None:
  106.                         entry = self._UpnpSource__db.entry_new(self._UpnpSource__entry_type, url)
  107.                     
  108.                     self._UpnpSource__db.set(entry, rhythmdb.PROP_TITLE, item.title)
  109.                     
  110.                     try:
  111.                         if item.artist is not None:
  112.                             self._UpnpSource__db.set(entry, rhythmdb.PROP_ARTIST, item.artist)
  113.                     except AttributeError:
  114.                         pass
  115.  
  116.                     
  117.                     try:
  118.                         if item.album is not None:
  119.                             self._UpnpSource__db.set(entry, rhythmdb.PROP_ALBUM, item.album)
  120.                     except AttributeError:
  121.                         pass
  122.  
  123.                     
  124.                     try:
  125.                         self.info('%r %r', item.title, item.originalTrackNumber)
  126.                         if item.originalTrackNumber is not None:
  127.                             self._UpnpSource__db.set(entry, rhythmdb.PROP_TRACK_NUMBER, int(item.originalTrackNumber))
  128.                     except AttributeError:
  129.                         pass
  130.  
  131.                     if duration is not None:
  132.                         (h, m, s) = duration.split(':')
  133.                         seconds = int(h) * 3600 + int(m) * 60 + int(s)
  134.                         self.info('%r %r:%r:%r %r', duration, h, m, s, seconds)
  135.                         self._UpnpSource__db.set(entry, rhythmdb.PROP_DURATION, seconds)
  136.                     
  137.                     if size is not None:
  138.                         self._UpnpSource__db.set(entry, rhythmdb.PROP_FILE_SIZE, int(size))
  139.                     
  140.                     self._UpnpSource__db.commit()
  141.                 
  142.             url is not None
  143.         
  144.  
  145.  
  146. gobject.type_register(UpnpSource)
  147.